package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseFragment;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.CurrentUser;
import com.isunland.managesystem.entity.PersonalUseableScore;
import com.isunland.managesystem.entity.StaffscoreTypeContent;
import com.isunland.managesystem.utils.MyDateUtil;
import java.util.Date;
import java.util.HashMap;
import org.json.JSONException;

/**
 * Created by wu on 2016/4/25.
 * 奖惩积分填报基类
 */
public class StaffScoreBaseFragment extends BaseFragment {

  protected static final int REQUEST_SCORE = 0;
  protected static final int REQUEST_SCORE_TYPE = 1;
  protected TextView mTv_addPersonalScore;//被奖惩人
  protected TextView mTv_addScoreType;//奖惩类型
  protected EditText meT_addScores;//奖惩分值
  protected EditText meT_plusScores;//扣减分值
  protected EditText mTv_addScoreDescription;//说明
  protected TextView mTv_staffDepartment;//人事所在部门
  protected LinearLayout mll_filScoreDetail;//

  protected TextView mTv_statusScore;//状态
  protected TextView mTv_registerScores;//登记人
  protected TextView mTv_registerTimeScore;//登记时间
  protected TextView mTv_checkorScore;//审核人
  protected TextView mTv_checkTimeScore;//审核时间
  protected LinearLayout mll_departmentScore;

  protected TextView mtv_scoreContent;//奖惩内容

  protected String mStaffName;
  protected String mjobNos;
  protected String mUsabeAddScore;//本月可派奖励积分
  protected String mUsablePlusScore;//本月可派扣减积分
  protected String mTypeName;//奖惩类别名
  protected String mTypeCode;//奖惩类别编码
  protected String mAdapteDepartment;//适合部门
  protected String mappOrgCodes;//适合部门
  protected String maxLimitScore;//最大限制分值
  protected String mRewardStdCom;//奖惩标准
  protected String mDeptCode;//部门编码
  protected String mRewardContent;//奖惩内容
  protected String minLimitScore;//最小分值
  protected String mScoreFactore;//分值系数

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setBack(true);
    VolleyPostGetScore();
  }

  /**
   * 获取所剩余可派奖励分、剩余可派惩罚分
   */
  protected void VolleyPostGetScore() {
    HashMap<String, String> params = new HashMap<>();
    params.put("memberCode", CurrentUser.newInstance(getActivity()).getMemberCode());
    params.put("staffId", CurrentUser.newInstance(getActivity()).getJobNumber());
    params.put("dataStatus", "publish");
    params.put("currentYearMonth", MyDateUtil.dateToString(new Date(), MyDateUtil.TYPE_DATE4));
    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_GET_ALL_SCORE);
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {

        PersonalUseableScore useableScore = new Gson().fromJson(arg0, PersonalUseableScore.class);
        mUsabeAddScore = useableScore.getCurrentRewvalue();
        mUsablePlusScore = useableScore.getCurrentPunvalue();
      }

      @Override public void onVolleyError(VolleyError arg0) {
      }
    });
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_add_score, container, false);
    mTv_addPersonalScore = (TextView) view.findViewById(R.id.tv_addPersonalScore);
    mTv_addScoreType = (TextView) view.findViewById(R.id.tv_addScoreType);
    meT_addScores = (EditText) view.findViewById(R.id.tv_addScores);
    meT_plusScores = (EditText) view.findViewById(R.id.tv_plusScores);
    mTv_addScoreDescription = (EditText) view.findViewById(R.id.tv_addScoreDescription);

    mTv_staffDepartment = (TextView) view.findViewById(R.id.tv_staffDepartment);
    mll_filScoreDetail = (LinearLayout) view.findViewById(R.id.ll_filScoreDetail);
    mTv_statusScore = (TextView) view.findViewById(R.id.tv_statusScore);
    mTv_registerScores = (TextView) view.findViewById(R.id.tv_registerScores);
    mTv_registerTimeScore = (TextView) view.findViewById(R.id.tv_registerTimeScore);
    mTv_checkorScore = (TextView) view.findViewById(R.id.tv_checkorScore);
    mTv_checkTimeScore = (TextView) view.findViewById(R.id.tv_checkTimeScore);
    mll_departmentScore = (LinearLayout) view.findViewById(R.id.ll_departmentScore);

    mtv_scoreContent = (TextView) view.findViewById(R.id.tv_scoreContent);

    mTv_addPersonalScore.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        Intent i = new Intent(getActivity(), SearchPersonMultipleActicity.class);
        i.putExtra(SearchPersonMultipleFragment.EXTRA_FROM,
            SearchPersonMultipleFragment.VALUE_COMMON);
        i.putExtra(SearchPersonMultipleFragment.EXTRA_JOB, mjobNos);
        startActivityForResult(i, REQUEST_SCORE);
      }
    });

    bindData();
    return view;
  }

  /**
   * 初始化数据
   */
  protected void bindData() {
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) {
      return;
    }

    if (requestCode == REQUEST_SCORE && data != null) {
      mStaffName = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_NAME);
      mjobNos = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_JOB);
      mDeptCode = data.getStringExtra(SearchPersonMultipleFragment.EXTRA_DEPCODE);
      mTv_addPersonalScore.setText(mStaffName);
    }
    if (requestCode == REQUEST_SCORE_TYPE && data != null) {
      StaffscoreTypeContent.ScoreTypeContent content =
          (StaffscoreTypeContent.ScoreTypeContent) data.getSerializableExtra(
              ScoreContentListFragment.EXTRA_VALUE);
      mTypeName = content.getRewardKindName();
      mTypeCode = content.getRewardKindCode();
      mAdapteDepartment = content.getAppOrgNames();
      mappOrgCodes = content.getAppOrgCodes();
      mRewardContent = content.getRewardContent();
      maxLimitScore = content.getMaxLimitScore();
      minLimitScore = content.getMinLimitScore();
      mScoreFactore = content.getScoreFactor();
      mRewardStdCom = content.getRewardStdCom();
      mTv_addScoreType.setText(mTypeName);
      if (!TextUtils.isEmpty(mRewardContent)) {
        mtv_scoreContent.setText(mRewardContent);
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }
}
